package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.bi.BHistoryActivity;
import com.stn.zjx.givingtreasure.activity.bi.BiMainActivity;
import com.stn.zjx.givingtreasure.bean.EthItem;
import com.stn.zjx.givingtreasure.domain.TokenListDomain;
import com.stn.zjx.givingtreasure.event.BiMainRefreshEvent;
import com.stn.zjx.givingtreasure.event.RefreshWalletEvent;
import com.stn.zjx.givingtreasure.fragment.bimain.BiAssetsFragment;
import com.stn.zjx.givingtreasure.table.WalletUser;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class TokenItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<EthItem> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public BiAssetsFragment context;
   public TokenItemListAdapter(BiAssetsFragment context) {
       inflater = LayoutInflater.from(context.activity);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_b_item, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       EthItem dataBean=dataBeanList.get(i);
          if(dataBean.getEthSymbolName().equals("ETH")){
              BiMainRefreshEvent event=new BiMainRefreshEvent();
               event.flag=BiMainRefreshEvent.TOTALASSETS;
               event.data=dataBean.getCny();
               EventBus.getDefault().post(event);
          }
       Glide.with(context)
               .asBitmap()
               .load(dataBean.getEthTokenIcon())
               .into(ih.icon_b);
           ih.name.setText(dataBean.getEthSymbolName());
           ih.b_tv.setText(dataBean.getBalance());
           ih.rmb_tv.setText("≈ ¥ "+dataBean.getCny());
         ih.rootView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if(context.walletUser==null){
                    return;
                 }
                 Bundle bundle=new Bundle();
                 bundle.putString("address",""+context.walletUser.getAddress());
                 bundle.putString("tokenAddress",dataBean.getEthTokenAddress());
                 bundle.putString("tokenSymbol",dataBean.getEthSymbolName());
                 bundle.putInt("tokenDecimal",dataBean.getEthTokenDecimal());
                 RxActivityTool.skipActivity(context.activity, BHistoryActivity.class,bundle);
             }
         });
   }


   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<EthItem> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }


   public void appendList(List<EthItem> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public ImageView icon_b;
           public TextView name;
           public TextView b_tv;
           public TextView rmb_tv;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            icon_b=itemView.findViewById(R.id.icon_b);
            name=itemView.findViewById(R.id.name);
            b_tv=itemView.findViewById(R.id.b_tv);
            rmb_tv=itemView.findViewById(R.id.rmb_tv);
        }
   }

}